//
//  VCLoginInPutView.m
//  VCShop
//
//  Created by VoyageCN on 16/8/19.
//  Copyright © 2016年 VoyageCN. All rights reserved.
//

#import "VCLoginInPutView.h"

@interface VCLoginInPutView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *textBackLabel;
@property (nonatomic, strong) UITextField *usernameText;
@property (nonatomic, strong) UITextField *passwordText;
@property (nonatomic, strong) UILabel *textLineLabel;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation VCLoginInPutView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.textBackLabel];
        [self addSubview:self.usernameText];
        [self addSubview:self.passwordText];
        [self addSubview:self.textLineLabel];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registerBtn];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    
    [_textBackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.mas_top).offset(16);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
        make.height.equalTo(@88);
    }];
    
    [_usernameText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.textBackLabel.mas_top);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
    
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@44);
        make.bottom.equalTo(weakSelf.textBackLabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
    
    [_textLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@0.5);
        make.left.right.equalTo(weakSelf.usernameText);
        make.centerY.equalTo(weakSelf.textBackLabel.mas_centerY);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(16);
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.equalTo(@35);
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.loginBtn.mas_bottom).offset(18);
        make.right.equalTo(weakSelf.loginBtn.mas_right);
        make.size.mas_equalTo(CGSizeMake(65, 34));
    }];
}

#pragma mark - 懒加载

- (UILabel *)textBackLabel {
    
    if (!_textBackLabel) {
        _textBackLabel = [[UILabel alloc] init];
        _textBackLabel.backgroundColor = [UIColor whiteColor];
        _textBackLabel.layer.borderWidth = 0.5;
        _textBackLabel.layer.borderColor = RGB(188, 188, 188).CGColor;
    }
    return _textBackLabel;
}

- (UITextField *)usernameText {
    
    if (!_usernameText) {
        _usernameText = [[UITextField alloc] init];
        _usernameText.delegate = self;
        _usernameText.placeholder = @"请输入手机号码";
        [_usernameText addTarget:self action:@selector(userNameTextChangeText:) forControlEvents:UIControlEventEditingChanged];
    }
    return _usernameText;
}

- (UITextField *)passwordText {
    
    if (!_passwordText) {
        _passwordText = [[UITextField alloc] init];
        _passwordText.delegate = self;
        _passwordText.placeholder  = @"请输入密码";
        _passwordText.secureTextEntry = YES;
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordText;
}

- (UILabel *)textLineLabel {
    
    if (!_textLineLabel) {
        _textLineLabel = [[UILabel alloc] init];
        _textLineLabel.backgroundColor = RGB(188, 188, 188);
    }
    return _textLineLabel;
}

- (UIButton *)loginBtn {
    
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.backgroundColor = RGB(31, 182, 241);
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIButton *)registerBtn {
    
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setTitle:@"免费注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:RGB(0, 124, 174) forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:16];
//        [_registerBtn addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.usernameText && range.location > 10) {
        return NO;
    }
    return YES;
}

#pragma mark - TextChange
//用户名修改内容时调用的方法

- (void)userNameTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length == 11 && _passwordText.text.length > 5) {
        _loginBtn.backgroundColor = RGB(56, 166, 241);
        _loginBtn.userInteractionEnabled = YES;
        _loginBtn.selected = YES;
    }else{
        _loginBtn.backgroundColor = RGB(229, 229, 229);
        _loginBtn.userInteractionEnabled = NO;
        _loginBtn.selected = NO;
    }
}
//密码输入框修改内容是调用的方法
- (void)passwordTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length > 5 && _usernameText.text.length == 11) {
        _loginBtn.backgroundColor = RGB(56, 166, 241);
        _loginBtn.userInteractionEnabled = YES;
    }else{
        _loginBtn.backgroundColor = RGB(229, 229, 229);
        _loginBtn.userInteractionEnabled = NO;
    }
}


#pragma mark - private

- (void)loginAction {
    
    if (_loginMethodBlock) {
        _loginMethodBlock(@{@"loginName":_usernameText.text,
                            @"Lpassword":_passwordText.text
                            });
    }
}
@end
